from django.db import models
from django.core.validators import MinValueValidator

class Route(models.Model):
    STATUS_CHOICES = [
        ('active', '运营中'),
        ('maintenance', '维护中'),
        ('closed', '已停运')
    ]
    
    # 核心字段
    route_id = models.CharField(max_length=20, unique=True, verbose_name="线路编号")
    name = models.CharField(max_length=100, verbose_name="线路名称")
    start_station = models.CharField(max_length=100, verbose_name="起始站")
    end_station = models.CharField(max_length=100, verbose_name="终点站")
    stops = models.JSONField(default=list, verbose_name="途经站点")
    distance_km = models.FloatField(validators=[MinValueValidator(0)], verbose_name="全程里程(公里)")
    estimated_time = models.IntegerField(validators=[MinValueValidator(0)], verbose_name="预计耗时(分钟)")
    
    # 运营信息
    operational_hours = models.CharField(max_length=20, verbose_name="运营时间段")
    base_price = models.DecimalField(max_digits=10, decimal_places=2, validators=[MinValueValidator(0)], verbose_name="基础票价")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active', verbose_name="运营状态")
    
    # 高级信息
    max_capacity = models.IntegerField(validators=[MinValueValidator(1)], verbose_name="单日最大班次数")
    emergency_contact = models.CharField(max_length=20, blank=True, verbose_name="紧急联系电话")
    notes = models.TextField(blank=True, verbose_name="内部备注")
    
    # 时间戳
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    
    class Meta:
        verbose_name = "线路"
        verbose_name_plural = "线路"
        permissions = [
            ("manage_route", "管理线路信息"),  # 调度员权限
        ]
        
    def __str__(self):
        return f"{self.name} ({self.route_id})"
